<template>
  <div id="login">
    <header>
      <img src="~@/images/register/logo.png" alt="">
    </header>

    <div class="loginarea">
      <div class="container">
        <div class="attention"><i class="el-icon-warning-outline"></i>校易网不会以任何理由要求您转账汇款，谨防诈骗。</div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名：" prop="username">
            <el-input v-model="ruleForm.username" :autofocus='true'></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input v-model="ruleForm.password" show-password></el-input>
          </el-form-item>

          <!-- <input type="button" value="提交" @click="login_submit(ruleForm.username, ruleForm.password)"> -->
          <input type="button" value="登录" class="btn" @click="login_submit(ruleForm.username, ruleForm.password)">
        </el-form>

        <div class="tip"><a href="#">忘记密码</a><a href="#">忘记用户名</a><router-link to="/register">免费注册</router-link></div>
      </div>
      
    </div>
  </div>
</template>

<script>
// import axios from 'axios'
import {request} from '@/network/request'

export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    login_submit(username, password) {
      let user = JSON.stringify({
        username,
        password
      });
      //后台数据库验证
      // axios.post('http://192.168.43.152:8555/xiaoyi/user/login', user, {
      //   headers: {
      //     "Content-Type": "application/json;charset=utf-8"  //头部信息
      //   }
      // }).then(res => {
      //   // console.log(res);
      //   if(res.data.flag) {
      //     this.$store.commit('changeLogin')
      //     this.$store.commit('changeUserId', res.data.data.items.userId)
      //     this.$store.commit('changeUserName', res.data.data.items.username)
      //     this.$router.push('/home')
      //   }
      // })

      request({
        url: '/user/login',
        method: 'post',
        data: user
      }).then(res => {
        if(res.data.flag) {
          // console.log(res);
          this.$store.commit('changeLogin')
          this.$store.commit('changeUserId', res.data.data.items.userId)
          this.$store.commit('changeUserName', res.data.data.items.username)
          this.$store.commit('changeUserImg', res.data.data.items.userImg)
          this.$message({
            message: '登陆成功！',
            type: 'success'
          });
          this.$router.push('/home')
        } else {
          // console.log(res);
          this.$message({
            message: '用户名或密码错误',
            type: 'error'
          });
        }
      }).catch(err => {
        console.log(err);
      })
    }
  }
}
</script>

<style scoped>
  #login {
    /* width: 60%; */
    margin: 0 auto;
  }

  #login header {
    height: 90px;
    border-bottom: 2px solid red;
    width: 60%;
    margin: 0 auto;
}

  #login header img {
    padding-top: 10px;
    height: 70px;
  }

  #login .loginarea {
    height: 550px;
    border: 1px solid #ccc;
    margin-top: 20px;

    background-image: url('~@/images/login/login2.jpg');
    background-position-x: right;

    position: relative;
  }

  #login .loginarea .container {
    width: 350px;
    height: 350px;
    /* padding: 20px 0 0 0; */

    text-align: center;

    background-color: #fff;

    position: absolute;
    right: 350px;
    bottom: 120px;
  }

  #login .loginarea .container .attention {
    background-color: #fff8f0;
    font-size: 13px;
    color: #999999;
    height: 50px;
    line-height: 50px;

    margin-bottom: 30px;
  }

  #login .loginarea .container .el-input {
    width: 200px;
    transform: translateX(-20px);
  }

  #login .loginarea .container .btn {
    width: 200px;
    height: 34px;
    background-color: red;
    color: #fff;
    font-size: 14px;
    margin-top: 30px;
    cursor: pointer;
  }

  #login .loginarea .container a {
    font-size: 13px;
    text-decoration: none;
    color: #6c6c6c;
    margin-right: 10px;
  }

  #login .loginarea .container a:hover {
    color: red;
  }

  #login .loginarea .container .tip {
    position: absolute;
    right: 10px;
    bottom: 10px;
  }
</style>